
import java.util.Arrays;
import java.util.Random;

public class MyPriorityQueue {


    private int[] array = new int[100];
    private int size = 0;



    public  void shiftDown(int[] array, int parent, int len) {

        int child = 2 * parent + 1;  //父节点的左孩子
        while (child <= len) {

            if (child + 1 <= len && array[child + 1] < array[child]) {
                child = child + 1;

            }

            if (array[child] < array[parent]) {
                swap( child, parent);
            }

            parent = child;
            child = 2 * child + 1;


        }


    }

    private  void swap( int child, int parent) {
        int tmp = array[child];
        array[child] = array[parent];
        array[parent] = tmp;

    }


    public  void shiftUp(int[] array, int child) {

        int parent = (child - 1) / 2;
        while (child > 0) {
            if (array[child] < array[parent]) {
                swap(child, parent);
                child = parent;
                parent = (child - 1) / 2;

            } else {
                break;
            }


        }
    }





    public void offer(int e) {
        array[size++] = e;
        shiftUp(array,size - 1);
    }

    public int poll() {
        int oldValue = array[0];
        array[0] = array[--size];
        shiftDown(array,0,size-1);
        return oldValue;
    }

    public int peek() {
        return array[0];
    }

    public static void main(String[] args) {
        MyPriorityQueue queue=new MyPriorityQueue();
        queue.offer(1);
        queue.offer(4);
        queue.offer(33);
        queue.offer(9);
        queue.offer(14);
        queue.offer(6);


        for (int i = 0; i < queue.size; i++) {
            System.out.print(queue.array[i]+" ");

        }

        System.out.println();

        queue.poll();
        for (int i = 0; i < queue.size; i++) {
            System.out.print(queue.array[i]+" ");

        }
    }

    }


